package com.javabi.htmlbuilder;

import java.awt.Color;

/**
 * A Document Builder.
 */
public abstract class DocumentBuilder<D extends DocumentBuilder<D>> {

	/** The builder. */
	protected final StringBuilder document = new StringBuilder();

	@Override
	public String toString() {
		return document.toString();
	}

	/**
	 * Append a named color.
	 * @param color the color name.
	 * @return this builder.
	 */
	@SuppressWarnings("unchecked")
	public D color(HTMLColorName color) {
		document.append(color.getName());
		return (D) this;
	}

	/**
	 * Append an RGB color.
	 * @param red the red.
	 * @param green the green.
	 * @param blue the blue.
	 * @return this builder.
	 */
	public D rgb(int red, int green, int blue) {
		return rgb(red, green, blue, false);
	}

	/**
	 * Append an RGB color.
	 * @param red the red.
	 * @param green the green.
	 * @param blue the blue.
	 * @param percent true for a percentage color.
	 * @return this builder.
	 */
	@SuppressWarnings("unchecked")
	public D rgb(int red, int green, int blue, boolean percent) {
		HTMLColor.appendRGB(document, red, green, blue, percent);
		return (D) this;
	}

	/**
	 * Append a hex color.
	 * @param red the red.
	 * @param green the green.
	 * @param blue the blue.
	 * @return this builder.
	 */
	public D color(int red, int green, int blue) {
		return color(red, green, blue, false);
	}

	/**
	 * Append a hex color.
	 * @param color the color.
	 * @return this builder.
	 */
	@SuppressWarnings("unchecked")
	public D color(HTMLColor color) {
		color.appendTo(document);
		return (D) this;
	}

	/**
	 * Append a hex color.
	 * @param red the red.
	 * @param green the green.
	 * @param blue the blue.
	 * @param single true for single digit, false for double digit.
	 * @return this builder.
	 */
	@SuppressWarnings("unchecked")
	public D color(int red, int green, int blue, boolean single) {
		HTMLColor.appendHex(document, red, green, blue, single);
		return (D) this;
	}

	/**
	 * Append a hex value.
	 * @param value the value.
	 * @return this builder.
	 */
	public D hex(int value) {
		return hex(value, false);
	}

	/**
	 * Append a hex value.
	 * @param value the value.
	 * @param single true for single digit, false for double digit.
	 * @return this builder.
	 */
	@SuppressWarnings("unchecked")
	public D hex(int value, boolean single) {
		HTMLColor.appendHex(document, value, single);
		return (D) this;
	}

	/**
	 * Append an RGB color.
	 * @param color the color.
	 * @return this builder.
	 */
	public D rgb(Color color) {
		return rgb(color.getRed(), color.getGreen(), color.getBlue());
	}

	/**
	 * Append a new line.
	 */
	public final void newLine() {
		document.append('\n');
	}

	/**
	 * Append a new line.
	 */
	public final void comma() {
		document.append(',');
	}

	/**
	 * Append a new line.
	 */
	public final void space() {
		document.append(' ');
	}

}
